#By Daoyu @2010-07-07
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import Permission
from django.forms import ModelForm
from django import forms

#-------------------------------------------------------------------------------
# The form for add new User. For admin.
#-------------------------------------------------------------------------------
class new_user_form(ModelForm):
    ROLE_CHOICE = (
     ('', '----------'),
     ('is_producer', 'Producer'),
     ('is_artist', 'Artist'),
     ('is_client', 'Client'),
    )

    password = forms.CharField()
    user_type = forms.ChoiceField(choices=ROLE_CHOICE)
    class Meta:
        model = User
        field = ('username', 'email', 'password', 'user_type')   
        exclude = ('is_staff', 'is_active',
                   'is_superuser', 'last_login', 'date_joined', 'groups',
                    'user_permissions')    
        
#-------------------------------------------------------------------------------
# The form for register new User. For anonymous user.
#-------------------------------------------------------------------------------
class register_user_form(ModelForm):
    password = forms.CharField()
    
    class Meta:
        model = User
        field = ('username', 'email', 'password')   
        exclude = ('is_staff', 'is_active',
                   'is_superuser', 'last_login', 'date_joined', 'groups',
                    'user_permissions')   
        
#-------------------------------------------------------------------------------
# The form for add new User.
#-------------------------------------------------------------------------------
class edit_user_form(ModelForm):
    password = forms.CharField()
    
    class Meta:
        model = User  
        exclude = ('password', 'is_staff', 'is_active', 'username',
                   'is_superuser', 'last_login', 'date_joined', 'groups',
                    'user_permissions') 

#-------------------------------------------------------------------------------
# The form for delete a user.
#-------------------------------------------------------------------------------
class delete_user_form(forms.Form):
    users = User.objects.all()
    user = forms.ModelChoiceField(queryset=users)

#-------------------------------------------------------------------------------
# The user changes his profile or something else.
#-------------------------------------------------------------------------------
class update(models.Model):
    time = models.DateTimeField(auto_now_add=True)
    content = models.CharField(max_length=150, blank=True)
    def __unicode__(self):
        return self.content[0:20]

#-------------------------------------------------------------------------------
# Set the artists unavailable period.
#-------------------------------------------------------------------------------
class User_expire(models.Model):
    user = models.ForeignKey(User, unique=True)
    start = models.DateField(blank=True, null=True)
    end = models.DateField(blank=True, null=True)

#-------------------------------------------------------------------------------
# User profile. Additional user's information.
#-------------------------------------------------------------------------------
class User_profile(models.Model):
    def __unicode__(self):
        return self.user.username
    
    user = models.ForeignKey(User, unique=True)
    
    role = models.CharField(max_length=30, blank=True)
    phone = models.CharField(max_length=15, blank=True)
    office = models.CharField(max_length=30, blank=True)
    photo = models.CharField(max_length=200, default="/site_media/img/anonymous.png")
    note = models.CharField(max_length=100, blank=True)
    
    company = models.CharField(max_length=100, blank=True)
    address = models.CharField(max_length=100, blank=True)
    rate = models.CharField(max_length=10, blank=True)
    link = models.CharField(max_length=100, blank=True)
    
    language = models.CharField(max_length=100, blank=True)
    customer_skills = models.CharField(max_length=100, blank=True)
    
    film = models.CharField(max_length=110, blank=True)
    client_text = models.CharField(max_length=100, blank=True)
    
    class Meta:
        permissions = (
            ('is_admin', 'is an administrator'),
            ('is_producer', 'is a Administrator '),
            ('is_artist', 'is an artist'),
            ('is_client', 'is an client'),
        )

#-------------------------------------------------------------------------------
# Where the video should be upload to.
#-------------------------------------------------------------------------------
def get_upload_to(instance, filename):
    return 'upload/video/' + str(instance.user.id) + \
    '/' + filename     

#-------------------------------------------------------------------------------
# User's video. Time for upload time. Text for shot description.
#-------------------------------------------------------------------------------
class video(models.Model):
    user = models.ForeignKey(User) #the user is moods when it is a moods video
    def __unicode__(self):
        return self.url
    
    title = models.CharField(max_length=300)
    url = models.CharField(max_length=300)
    time = models.DateTimeField(auto_now_add=True)
    text = models.CharField(max_length=300)
    type = models.IntegerField()

#-------------------------------------------------------------------------------
# The upload new video request for an artist.
#-------------------------------------------------------------------------------
class Upload_request(models.Model):
    user = models.ForeignKey(User)
    def __unicode__(self):
        return self.user.username
    
    text = models.CharField(max_length=300)
    
class Pwd(models.Model):
    user = models.ForeignKey(User)
    pwd = models.CharField(max_length=30)
    def __unicode__(self):
        return self.user.username
    
#-------------------------------------------------------------------------------
# The categories of user.
#-------------------------------------------------------------------------------
class Category(models.Model):
    name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name
    
#-------------------------------------------------------------------------------
# The skill of a given category.
#-------------------------------------------------------------------------------
class Skill(models.Model):
    name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name
    
    category = models.ForeignKey(Category)

#-------------------------------------------------------------------------------
# Each user's skill
#-------------------------------------------------------------------------------
class Skill_user(models.Model):
    skill = models.ForeignKey(Skill)
    user = models.ForeignKey(User)

#-------------------------------------------------------------------------------
# The tool of a given category.
#-------------------------------------------------------------------------------
class Tool(models.Model):
    name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name
    
    category = models.ForeignKey(Category)

#-------------------------------------------------------------------------------
# Each user's tool
#-------------------------------------------------------------------------------
class Tool_user(models.Model):
    tool = models.ForeignKey(Tool)
    user = models.ForeignKey(User)

#-------------------------------------------------------------------------------
# The tags of a user. Tags are organized by categories.
#-------------------------------------------------------------------------------
class Tag(models.Model):
    name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name
    
    category = models.ForeignKey(Category)

#-------------------------------------------------------------------------------
# Each user's tag.
#-------------------------------------------------------------------------------
class Tag_user(models.Model):
    tag = models.ForeignKey(Tag)
    user = models.ForeignKey(User)
    
class category_skill_tool:    
    def __init__(self, cat):
        self.category = cat
        self.skls , self.tls = [], []
    
    def add_skill(self, skill):
        self.skls.append(skill)
    
    def add_tool(self, tool):
        self.tls.append(tool)
        
    def __unicode__(self):
        return self.category
    

#-------------------------------------------------------------------------------
# A class that related categories with tags, skills, tools.
#-------------------------------------------------------------------------------
class skill_tool_tag:
    def __init__(self, cat):
        self.category = cat
        self.tags, self.tools, self.skills = [], [], []

    def add_tag(self, tag):
        self.tags.append(tag)

    def add_tool(self, tool):
        self.tools.append(tool)

    def add_skill(self, skill):
        self.skills.append(skill)
    
#-------------------------------------------------------------------------------
# The tags of a video. Tags are organized by categories.
#-------------------------------------------------------------------------------
class VTag(models.Model):
    name = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name
    
    category = models.ForeignKey(Category)

#-------------------------------------------------------------------------------
# Each video's tag.
#-------------------------------------------------------------------------------
class VTag_video(models.Model):
    tag = models.ForeignKey(VTag)
    video = models.ForeignKey(video)
